<?php
// +----------------------------------------------------------------------
// |收藏模型
// +----------------------------------------------------------------------
// |Data:2016-06-27 下午4:21
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Wap\Model;
use Common\Model\CommonModel;

class CollectionModel extends CommonModel{
    protected $tableName = 'collection';

    /**
     * 获取收藏列表
     * @param $map
     * @param $ord
     * @return bool
     */
    public function getCollectionList($map,$ord){
        if(empty($map) || !is_array($map)) return false;
        $list = M()->table(C('DB_PREFIX').'shop as s')
                ->join(C('DB_PREFIX').'collection as c on s.id=c.shop_id')
                ->field('s.id,s.location,s.uid,s.title,s.pic,s.desc,s.sales_time,s.deviler_price,s.free_price')
                ->where($map)
                ->order($ord)
                ->select();
        return $list;
    }

    /**
     * 检测是否有
     * @param $shop_id
     * @param $uid
     * @return bool|mixed
     */
    public function isHas($shop_id,$uid){
        if(empty($shop_id) || empty($uid)) return false;
        return $this->where('shop_id='.$shop_id.' and uid='.$uid)->getField('id');
    }

    /**
     * 操作收藏和取消收藏
     * @param $shop_id 店铺ID
     * @param $uid 用户uid
     * @param int $type 1收藏,2取消收藏
     * @return bool|mixed
     */
    public function doCollection($shop_id,$uid,$type=1){
        if(empty($shop_id)) return false;
        if($type == 1){
            $data = array(
                'shop_id'=>$shop_id,
                'uid'=>$uid,
                'add_time'=>time()
            );
            $result = $this->add($data);
        }else{
            $result = $this->where('shop_id='.$shop_id)->delete();
        }
        return $result;
    }
}
?>